package com.crealth.handler;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crealth.ConstantDefine;
import com.crealth.db.ConsumerProductDb;
import com.crealth.db.MonthPlanDb;
import com.crealth.db.TeamDb;
import com.crealth.db.UserDb;
import com.crealth.db.WeekPlanDb;
import com.crealth.memcached.LoanContractCached;
import com.crealth.memcached.UserCached;
import com.crealth.model.ConsumerProductModel;
import com.crealth.model.LoanContractModel;
import com.crealth.model.MonthPlanModel;
import com.crealth.model.TeamModel;
import com.crealth.model.UserModel;
import com.crealth.model.WeekPlanModel;
import com.crealth.msg.NetMessageData;
import com.crealth.utils.StringUtil;
import com.crealth.utils.Utils;

public class TeamHandler {
	private static Logger errorLog = Logger.getLogger("error_log");
	
	public static NetMessageData startMonthPlan(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() != ConstantDefine.Team_Manager) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		int teamID = user.getTeamID();
		if (teamID <= 0) {
			errorLog.error("userID: " + userID + "  teamID is 0");
			return null;
		}
		TeamModel team = TeamDb.selectTeamByID(teamID);
		if (team == null) {
			errorLog.error("userID: " + userID + " teamID: " + teamID + "  TeamModel is null");
			return null;
		}
		int monthPlan = team.getMonthPlan();
		int setPlan = 0;
		if (monthPlan == 0) {
			setPlan = 1;			
		}		
		TeamDb.updateMonthPlanByTeamID(teamID, setPlan);
		
		resJson.put("resCode", 0);
		resJson.put("setPlan", setPlan);
		resJson.put("tp", "month");
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		ResponseMsgQueue.sendMsgToTream(teamID, sendMsg, true);
		
		return null;
	}
	
	public static NetMessageData startWeekPlan(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		if (user.getPosition() != ConstantDefine.Team_Manager) {
			resJson.put("resCode", ErrorCode.ERROR_Limited_Auth);
			sendMsg.setMsgBody(resJson.toJSONString());
			return sendMsg;
		}
		
		int teamID = user.getTeamID();
		if (teamID <= 0) {
			errorLog.error("userID: " + userID + "  teamID is 0");
			return null;
		}
		TeamModel team = TeamDb.selectTeamByID(teamID);
		if (team == null) {
			errorLog.error("teamID: " + teamID + "  TeamModel is null");
			return null;
		}
		int weekPlan = team.getWeekPlan();
		int setPlan = 0;
		if (weekPlan == 0) {
			setPlan = 1;
		}
		TeamDb.updateWeekPlanByTeamID(teamID, setPlan);
		
		resJson.put("resCode", 0);
		resJson.put("setPlan", setPlan);
		resJson.put("tp", "week");
		sendMsg.setMsgBody(resJson.toJSONString());
		sendMsg.setMsgCmd(revData.getMsgCmd());
		ResponseMsgQueue.sendMsgToTream(teamID, sendMsg, true);
		
		return null;
	}

	public static NetMessageData saveWeekPlan(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();
		int userID = msgJson.getIntValue("id");
		String tp = msgJson.getString("tp");
		UserModel user = UserCached.selectUserByID(userID);
		if (tp.equals("week")) {
			int week = 0;  //cal.get(Calendar.WEEK_OF_YEAR);
			WeekPlanModel weekPlan = new WeekPlanModel();
			weekPlan.setUserID(userID);
			weekPlan.setWeek(week);
			weekPlan.parseFromJson(msgJson);
			weekPlan.setPlanTm( new Timestamp(System.currentTimeMillis()) );
			WeekPlanModel existModel = WeekPlanDb.selectUserWeekPlan(userID);
			if (existModel == null) {
				WeekPlanDb.insertWeekPlan(weekPlan);
			}
			else {
				WeekPlanDb.updateWeekPlan(weekPlan);
			}
			resJson.put("weekPlan", weekPlan.toJSONObject());
		} else {
			int month = 0; //cal.get(Calendar.MONTH);
			MonthPlanModel monthPlan = new MonthPlanModel();
			monthPlan.setUserID(userID);
			monthPlan.setMonth(month);
			monthPlan.parseFromJson(msgJson);
			monthPlan.setPlanTm( new Timestamp(System.currentTimeMillis()) );
			MonthPlanModel existMonth = MonthPlanDb.selectUserMonthPlan(userID);
			if (existMonth == null) {
				MonthPlanDb.insertMonthPlan(monthPlan);
			}
			else {
				MonthPlanDb.updateMonthPlan(monthPlan);
			}
			resJson.put("monthPlan", monthPlan.toJSONObject());
		}
		resJson.put("resCode", 0);
		resJson.put("opUser", userID);
		sendMsg.setMsgBody(resJson.toJSONString());
		int cmd = revData.getMsgCmd();
		sendMsg.setMsgCmd(cmd);
		ResponseMsgQueue.addMsgToQueue(cmd, user.getLeaderID(), sendMsg);
		ResponseMsgQueue.sendBranchCompanyMgr(userID, user.getCompanyID(), sendMsg);
		ResponseMsgQueue.sendCompanyTopMgr(userID, sendMsg);
		
		return sendMsg;
	}
	
	public static NetMessageData obtainSalesmanTeamPlan(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();
		int userID = msgJson.getIntValue("id");
		//UserModel user = UserCached.selectUserByID(userID);		
		WeekPlanModel existModel = WeekPlanDb.selectUserWeekPlan(userID);
		if (existModel != null) {
			resJson.put("weekPlan", existModel.toJSONObject());
		}
		MonthPlanModel monthModel = MonthPlanDb.selectUserMonthPlan(userID);
		if (monthModel != null) {
			resJson.put("monthPlan", monthModel.toJSONObject());
		}
		resJson.put("resCode", 0);		
		sendMsg.setMsgBody(resJson.toJSONString());
		
		return sendMsg;
	}
	
	public static NetMessageData obtainTeamMonthPlan(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();
		Integer userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		
		List<MonthPlanModel> monthPlanls = null;
		long monthStartTm = Utils.getCurrentMonthStartTm();
		Timestamp monthTm = new Timestamp(monthStartTm);
		String toMonth = Utils.getCurrentYearMonth();
		if (msgJson.containsKey("seaMonth") && 
			!msgJson.getString("seaMonth").equals(toMonth) ) {
			String seaMonth = msgJson.getString("seaMonth");
			monthTm = Utils.getTmFromString(seaMonth);
			if (monthTm == null) {
				monthTm = new Timestamp(monthStartTm);
			}
			List<Integer> userIDs = null;
			if (user.getPosition() >= ConstantDefine.Head_Manager) {				
				userIDs = UserDb.selectAllSalesManAndTeamMgrIDs();
			}
			else {
				userIDs = UserDb.selectUnderlingIDs(userID);
				if (user.getPosition() == ConstantDefine.Team_Manager) {
					if ( userIDs.indexOf(userID) < 0 ) {
						userIDs.add(userID);
					}
				}
			}
			if (userIDs != null && userIDs.size() > 0) {
				monthPlanls = new ArrayList<MonthPlanModel>();
				for (Integer curID : userIDs) {
					MonthPlanModel mPlan = new MonthPlanModel();
					mPlan.setUserID(curID);
					monthPlanls.add(mPlan);
				}
			}
		} else {
			if (user.getPosition() >= ConstantDefine.Head_Manager) {
				monthPlanls = MonthPlanDb.selectTeamMonthPlan();
			}
			else {
				List<Integer> userIDs = UserDb.selectUnderlingIDs(userID);				
				if (user.getPosition() == ConstantDefine.Team_Manager) {
					userIDs.add(userID);
				}
				String strIDs = StringUtil.mergeListIntegerToString(userIDs, ",");
				monthPlanls = MonthPlanDb.selectTeamMonthPlanByIDs(strIDs);
			}		
			if (monthPlanls == null || monthPlanls.size() == 0) {
				return null;
			}
		}
		Timestamp monthEndTm = Utils.getMonthEndTm(monthTm);
		List<ConsumerProductModel> signedProductls = ConsumerProductDb.selectSignedConsumerProduct(monthTm, monthEndTm);
		Map<Integer, Integer> userSignedMap = new HashMap<Integer, Integer>();
		for (ConsumerProductModel product : signedProductls) {
			Integer productUserID = product.getUserID();
			if (userSignedMap.containsKey(productUserID)) {
				Integer value = userSignedMap.get(productUserID);
				userSignedMap.put(productUserID, value+1);
			} else {
				userSignedMap.put(productUserID, 1);
			}			
		}
		List<ConsumerProductModel> extendLoanProductls = ConsumerProductDb.selectExtendLoanConsumerProduct(monthTm, monthEndTm);
		List<ConsumerProductModel> brokerageProductls = ConsumerProductDb.selectBrokerageConsumerProduct(monthTm, monthEndTm);
		//  放款金额
		Map<Integer, Integer> userExtendLoanMap = new HashMap<Integer, Integer>();
		//  放款件数
		Map<Integer, Integer> userExtendLoanCntMap = new HashMap<Integer, Integer>();
		for (ConsumerProductModel product : extendLoanProductls) {
			Integer productUserID = product.getUserID();
			int bankExtendLoan = 0;			
			try {
				bankExtendLoan = Integer.valueOf(product.getBankExtendLoan());
			} catch (NumberFormatException e) {				
			}
			
			if (userExtendLoanMap.containsKey(productUserID)) {
				Integer value = userExtendLoanMap.get(productUserID);
				userExtendLoanMap.put(productUserID, value+bankExtendLoan);
				int cntValue = userExtendLoanCntMap.get(productUserID);
				userExtendLoanCntMap.put(productUserID, cntValue+1);				
			} else {
				userExtendLoanMap.put(productUserID, bankExtendLoan);
				userExtendLoanCntMap.put(productUserID, 1);
			}
		}
		// 创收   佣金
		Map<Integer, Double> userBrokerageMap = new HashMap<Integer, Double>();
		// 基本提成
		Map<Integer, Double> userBasePushMoneyMap = new HashMap<Integer, Double>();
		//  额外提成
		Map<Integer, Double> userExtraPushMoneyMap = new HashMap<Integer, Double>();
		for (ConsumerProductModel product : brokerageProductls) {
			Integer productUserID = product.getUserID();			
			double brokerage = 0;  // 佣金
			double basePushMoney = 0;  // 基本提成
			double extraPushMoney = 0;  // 额外提成			
			try {
				brokerage = Double.valueOf(product.getBrokerage());
			} catch (NumberFormatException e) {				
			}
			try {
				basePushMoney = Double.valueOf(product.getBasePushMoney());
			} catch (NumberFormatException e) {				
			}
			try {
				extraPushMoney = Double.valueOf(product.getExtraPushMoney());
			} catch (NumberFormatException e) {				
			}
			if (userBrokerageMap.containsKey(productUserID)) {
				double brokerageValue = userBrokerageMap.get(productUserID);
				userBrokerageMap.put(productUserID, brokerageValue+brokerage);
				double baseValue = userBasePushMoneyMap.get(productUserID);
				userBasePushMoneyMap.put(productUserID, baseValue+basePushMoney);
				double extraValue = userExtraPushMoneyMap.get(productUserID);
				userExtraPushMoneyMap.put(productUserID, extraValue+extraPushMoney);
			} else {				
				userBrokerageMap.put(productUserID, brokerage);
				userBasePushMoneyMap.put(productUserID, basePushMoney);
				userExtraPushMoneyMap.put(productUserID, extraPushMoney);
			}
		}
		
		JSONArray jsonArray = new JSONArray();
		for (MonthPlanModel monthPlan : monthPlanls) {
			int planUserID = monthPlan.getUserID();
			if (userSignedMap.containsKey(planUserID)) {
				int signedCnt = userSignedMap.get(planUserID);
				monthPlan.setSignedCnt(signedCnt);
			}
			if (userExtendLoanCntMap.containsKey(planUserID)) {
				int extendLoanCnt = userExtendLoanCntMap.get(planUserID);
				monthPlan.setExtendLoanCnt(extendLoanCnt);
			}
			if (userExtendLoanMap.containsKey(planUserID)) {
				int extendLoanMoney = userExtendLoanMap.get(planUserID);
				monthPlan.setExtendLoanMoney(extendLoanMoney);
			}
			if (userBrokerageMap.containsKey(planUserID)) {
				double brokerageMoney = userBrokerageMap.get(planUserID);
				monthPlan.setBrokerageMoney(brokerageMoney);
			}
			if (userBasePushMoneyMap.containsKey(planUserID)) {
				double basePushMoney = userBasePushMoneyMap.get(planUserID);
				monthPlan.setBasePushMoney(basePushMoney);
			}
			if (userExtraPushMoneyMap.containsKey(planUserID)) {
				double extraPushMoney = userExtraPushMoneyMap.get(planUserID);
				monthPlan.setExtraPushMoney(extraPushMoney);
			}
			jsonArray.add(monthPlan.toJSONObject());
		}
		
		resJson.put("monthPlanArr", jsonArray);
		resJson.put("resCode", 0);		
		sendMsg.setMsgBody(resJson.toJSONString());
		
		return sendMsg;
	}
	
	public static NetMessageData beforeTeamWeekPlan(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();		
		int preWeek = msgJson.getIntValue("preWeek");
		if (preWeek <= 0) {
			return null;
		}
		
		long weekStartTm = Utils.getCurrentWeekStartTm() - preWeek * ConstantDefine.OneWeekMilliSecond;
		Timestamp weekTm = new Timestamp(weekStartTm);
		Timestamp weekEndTm = Utils.getWeekEndTm(weekTm);
		List<ConsumerProductModel> signedProductls = ConsumerProductDb.selectSignedExtendLoanProduct(weekTm, weekEndTm);
		int userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		List<WeekPlanModel> weekPlanls = new ArrayList<WeekPlanModel>();
		Set<Integer> productUserIDSet = new HashSet<Integer>();
		if (signedProductls.size() == 0) {
			return null;
		}
		
		Map<Integer, Integer> userExtendLoanMap = new HashMap<Integer, Integer>();
		Map<Integer, Integer> userSignedMap = new HashMap<Integer, Integer>();
		Map<Integer, List<Integer> > userSignedConsumerMap = new HashMap<Integer, List<Integer> >();
		Map<Integer, List<Integer> > userExtendLoanConsumerMap = new HashMap<Integer, List<Integer> >();
		for (ConsumerProductModel curProduct : signedProductls) {
			Integer productUserID = curProduct.getUserID();
			if (productUserIDSet.add(productUserID)) {
				WeekPlanModel weekPlan = new WeekPlanModel();
				weekPlan.setUserID(productUserID);
				weekPlanls.add(weekPlan);
			}
			String extendLoan = curProduct.getBankExtendLoan();
			if (extendLoan.length() > 4 ) {
				int bankExtendLoan = 0;
				List<Integer> consumerIDls = null;
				try {
					bankExtendLoan = Integer.valueOf(extendLoan);
				} catch (NumberFormatException e) {				
				}
				if (userExtendLoanMap.containsKey(productUserID)) {
					Integer value = userExtendLoanMap.get(productUserID);
					userExtendLoanMap.put(productUserID, value+bankExtendLoan);
					consumerIDls = userExtendLoanConsumerMap.get(productUserID);
				} else {
					userExtendLoanMap.put(productUserID, bankExtendLoan);
					consumerIDls = new ArrayList<Integer>();
					userExtendLoanConsumerMap.put(productUserID, consumerIDls);
				}
				int contractID = curProduct.getContractID();
				LoanContractModel contract = LoanContractCached.selectLoanContractByID(contractID);
				if (contract != null) {
					consumerIDls.add(contract.getConsumerInfoID());
				}
			} else {
				List<Integer> consumerIDls = null;
				if (userSignedMap.containsKey(productUserID)) {
					Integer value = userSignedMap.get(productUserID);
					userSignedMap.put(productUserID, value+1);
					consumerIDls = userSignedConsumerMap.get(productUserID);
				} else {
					userSignedMap.put(productUserID, 1);
					consumerIDls = new ArrayList<Integer>();
					userSignedConsumerMap.put(productUserID, consumerIDls);
				}
				int contractID = curProduct.getContractID();
				LoanContractModel contract = LoanContractCached.selectLoanContractByID(contractID);
				if (contract != null) {
					consumerIDls.add(contract.getConsumerInfoID());
				}
			}
		}
			
		JSONArray jsonArray = new JSONArray();
		if (user.getPosition() >= ConstantDefine.Head_Manager) {
			for (WeekPlanModel weekPlan : weekPlanls) {
				Integer curUserID = weekPlan.getUserID();
				if (userSignedMap.containsKey(curUserID)) {
					weekPlan.setWeekSignedCnt(userSignedMap.get(curUserID));
					List<Integer> consumerIDls = userSignedConsumerMap.get(curUserID);
					if (consumerIDls != null && consumerIDls.size() > 0) {
						String strids = StringUtil.mergeListIntegerToString(consumerIDls, ",");
						weekPlan.setSignedConuserIDs(strids);
					}
				}
				if (userExtendLoanMap.containsKey(curUserID)) {
					weekPlan.setWeekExtendLoanCnt(userExtendLoanMap.get(curUserID));
					List<Integer> consumerIDls = userExtendLoanConsumerMap.get(curUserID);
					if (consumerIDls != null && consumerIDls.size() > 0) {
						String strids = StringUtil.mergeListIntegerToString(consumerIDls, ",");
						weekPlan.setExtendConuserIDs(strids);
					}
				}
				jsonArray.add(weekPlan.toJSONObject());
			}				
		}
		else {
			List<Integer> userIDs = UserDb.selectUnderlingIDs(userID);
			if (user.getPosition() == ConstantDefine.Team_Manager) {
				userIDs.add(userID);
			}
			String strIDs = StringUtil.mergeListIntegerToString(userIDs, ",");
			for (WeekPlanModel weekPlan : weekPlanls) {
				Integer curUserID = weekPlan.getUserID();
				String strUserID = "" + curUserID;
				if (strIDs.indexOf(strUserID) < 0) {
					continue;
				}
				if (userSignedMap.containsKey(curUserID)) {
					weekPlan.setWeekSignedCnt(userSignedMap.get(curUserID));
					List<Integer> consumerIDls = userSignedConsumerMap.get(curUserID);
					if (consumerIDls != null && consumerIDls.size() > 0) {
						String strids = StringUtil.mergeListIntegerToString(consumerIDls, ",");
						weekPlan.setSignedConuserIDs(strids);
					}
				}
				if (userExtendLoanMap.containsKey(curUserID)) {
					weekPlan.setWeekExtendLoanCnt(userExtendLoanMap.get(curUserID));
					List<Integer> consumerIDls = userExtendLoanConsumerMap.get(curUserID);
					if (consumerIDls != null && consumerIDls.size() > 0) {
						String strids = StringUtil.mergeListIntegerToString(consumerIDls, ",");
						weekPlan.setExtendConuserIDs(strids);
					}
				}
				jsonArray.add(weekPlan.toJSONObject());
			}
		}
		
		resJson.put("weekPlanArr", jsonArray);
		resJson.put("resCode", 0);
		resJson.put("preWeek", preWeek);
		sendMsg.setMsgBody(resJson.toJSONString());
		
		return sendMsg;
	}
	
	public static NetMessageData obtainTeamWeekPlan(NetMessageData revData) {
		NetMessageData sendMsg = new NetMessageData();
		JSONObject msgJson = revData.getRevDataMsgJson();
		JSONObject resJson = new JSONObject();
		if (msgJson.containsKey("preWeek")) {
			return TeamHandler.beforeTeamWeekPlan(revData);
		}
		Integer userID = msgJson.getIntValue("id");
		UserModel user = UserCached.selectUserByID(userID);
		List<WeekPlanModel> weekPlanls = null;
		if (user.getPosition() >= ConstantDefine.Head_Manager) {
			weekPlanls = WeekPlanDb.selectTeamWeekPlan();
		}
		else {
			List<Integer> userIDs = UserDb.selectUnderlingIDs(userID);
			if (userIDs.size() == 0) {
				return null;
			}
			if (user.getPosition() == ConstantDefine.Team_Manager) {
				if (userIDs.indexOf(userID) < 0) {
					userIDs.add(userID);
				}
			}
			String strIDs = StringUtil.mergeListIntegerToString(userIDs, ",");			
			weekPlanls = WeekPlanDb.selectTeamWeekPlanByIDs(strIDs);
		}
		if (weekPlanls == null || weekPlanls.size() == 0) {
			return null;
		}
		
		long weekStartTm = Utils.getCurrentWeekStartTm();
		Timestamp weekTm = new Timestamp(weekStartTm);
		Timestamp weekEndTm = Utils.getWeekEndTm(weekTm);
		List<ConsumerProductModel> signedProductls = ConsumerProductDb.selectSignedConsumerProduct(weekTm, weekEndTm);
		Map<Integer, Integer> userSignedMap = new HashMap<Integer, Integer>();
		Map<Integer, List<Integer> > userSignedConsumerMap = new HashMap<Integer, List<Integer> >();
		for (ConsumerProductModel product : signedProductls) {
			Integer productUserID = product.getUserID();
			List<Integer> userContractls = null;
			if (userSignedMap.containsKey(productUserID)) {
				Integer value = userSignedMap.get(productUserID);
				userSignedMap.put(productUserID, value+1);
				userContractls = userSignedConsumerMap.get(productUserID);
			} else {
				userSignedMap.put(productUserID, 1);
				userContractls = new ArrayList<Integer>();				
				userSignedConsumerMap.put(productUserID, userContractls);
			}
			int contractID = product.getContractID();
			LoanContractModel contract = LoanContractCached.selectLoanContractByID(contractID);
			if (contract != null) {
				userContractls.add(contract.getConsumerInfoID());
			}
		}
		List<ConsumerProductModel> extendLoanProductls = ConsumerProductDb.selectExtendLoanConsumerProduct(weekTm, weekEndTm);
		Map<Integer, Integer> userExtendLoanMap = new HashMap<Integer, Integer>();
		Map<Integer, List<Integer> > userExtendLoanConsumerMap = new HashMap<Integer, List<Integer> >();
		for (ConsumerProductModel product : extendLoanProductls) {
			Integer productUserID = product.getUserID();
			List<Integer> consumerIDls = null;
			int bankExtendLoan = 0;
			try {
				bankExtendLoan = Integer.valueOf(product.getBankExtendLoan());
			} catch (NumberFormatException e) {				
			}
			if (userExtendLoanMap.containsKey(productUserID)) {
				Integer value = userExtendLoanMap.get(productUserID);
				userExtendLoanMap.put(productUserID, value+bankExtendLoan);
				consumerIDls = userExtendLoanConsumerMap.get(productUserID);
			} else {
				userExtendLoanMap.put(productUserID, bankExtendLoan);
				consumerIDls = new ArrayList<Integer>();
				userExtendLoanConsumerMap.put(productUserID, consumerIDls);
			}
			int contractID = product.getContractID();
			LoanContractModel contract = LoanContractCached.selectLoanContractByID(contractID);
			if (contract != null) {
				consumerIDls.add(contract.getConsumerInfoID());
			}
		}
		JSONArray jsonArray = new JSONArray();
		for (WeekPlanModel weekPlan : weekPlanls) {
			Integer curUserID = weekPlan.getUserID();
			if (userSignedMap.containsKey(curUserID)) {
				weekPlan.setWeekSignedCnt(userSignedMap.get(curUserID));
				List<Integer> consumerIDls = userSignedConsumerMap.get(curUserID);
				if (consumerIDls != null && consumerIDls.size() > 0) {
					String strids = StringUtil.mergeListIntegerToString(consumerIDls, ",");
					weekPlan.setSignedConuserIDs(strids);
				}
			}
			if (userExtendLoanMap.containsKey(curUserID)) {
				weekPlan.setWeekExtendLoanCnt(userExtendLoanMap.get(curUserID));
				List<Integer> consumerIDls = userExtendLoanConsumerMap.get(curUserID);
				if (consumerIDls != null && consumerIDls.size() > 0) {
					String strids = StringUtil.mergeListIntegerToString(consumerIDls, ",");
					weekPlan.setExtendConuserIDs(strids);
				}
			}
			jsonArray.add(weekPlan.toJSONObject());
		}
		resJson.put("weekPlanArr", jsonArray);
		resJson.put("resCode", 0);		
		sendMsg.setMsgBody(resJson.toJSONString());
		
		return sendMsg;
	}
	
}
